package com.example.activity;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.example.R;
import com.example.common.GlobalConstant;
import com.example.common.GlobalVariable;
import com.example.modle.Mp3Info;

/**
 * Created with IntelliJ IDEA.
 * User: Jasic
 * Date: 12-11-29
 */
public class PlayerActivity extends Activity {

    private PlayStatus status = PlayStatus.stopped;

    private MediaPlayer mediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        super.setContentView(R.layout.playerlayout);

        this.init();
    }

    private void init() {

        Intent intent = getIntent();
        Mp3Info mp3Info = (Mp3Info) intent.getSerializableExtra("mp3");

        Button play = (Button) findViewById(R.id.play);
        Button stop = (Button) findViewById(R.id.stop);

        View.OnClickListener listener = new PlayOnClickListener(mp3Info);

        play.setOnClickListener(listener);
        play.setOnClickListener(listener);
    }


    /**
     * 播放监听器
     */
    class PlayOnClickListener implements View.OnClickListener {

        private Mp3Info mp3;

        private Uri uri;

        PlayOnClickListener(Mp3Info mp3) {

            this.mp3 = mp3;

            this.uri = Uri.parse(GlobalVariable.MP3_ROOT + mp3.getMp3_Name());
        }

        @Override
        public void onClick(View v) {

            switch (status) {

                /**
                 * 播放状态则点击后为暂时
                 */
                case playing: {
                    mediaPlayer.pause();
                    ((Button) v).setText(R.string.pause);
                    status = PlayStatus.pause;
                }

                /**
                 * 暂停状态则继续播放
                 */
                case pause: {
                    mediaPlayer.start();
                    status = PlayStatus.playing;
                    break;
                }

                /**
                 * 状态为停止则播放
                 */
                case stopped: {

                    mediaPlayer = MediaPlayer.create(PlayerActivity.this, this.uri);
                    mediaPlayer.setLooping(false);
                    mediaPlayer.start();

                    ((Button)v).setText(R.string.pause);
                    break;
                }
            }

        }
    }

    /**
     * 停止监听器
     */
    class StopOnClickListener implements View.OnClickListener {


        @Override
        public void onClick(View v) {

        }
    }

    enum PlayStatus {
        playing, pause, stopped;

        PlayStatus() {
        }
    }


}
